package com.npy.reggie.contorller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.npy.reggie.config.BaseConText;
import com.npy.reggie.doman.AddressBook;
import com.npy.reggie.doman.R;
import com.npy.reggie.service.AddressBookService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/addressBook")
public class AddressBookController {

    @Autowired
    private AddressBookService addressBookService;

    @GetMapping("/list")
    public R<List<AddressBook>> addressBookList(){

        List<AddressBook> list = addressBookService.list();

        return R.success(list);

    }

    @GetMapping ("/default")
    public R<AddressBook> addressBookR (){

        return R.success(addressBookService.getOne(new QueryWrapper<AddressBook>().lambda().eq(AddressBook::getIsDefault,1)));
    }

    @PostMapping
    public R<String> addAddressBook(@RequestBody AddressBook addressBook){
        addressBook.setUserId((Long) BaseConText.getCurrentId());
        addressBookService.save(addressBook);
        return R.success("添加成功");
    }

    @GetMapping("/{id}")
    public R<AddressBook> ReAddressBook(@PathVariable Long id){

        return R.success(addressBookService.getById(id));
    }

    @PutMapping
    public R<String> updateAddressBook(@RequestBody AddressBook addressBook){
        addressBookService.updateById(addressBook);
        return R.success("修改成功");
    }

    @PutMapping("/default")
    public R<String> updateDefault(@RequestParam(required = false , defaultValue = "-1") Long id){

        addressBookService.updateDefaultValue(id);

        return R.success("修改成功");
    }



    @DeleteMapping
    public R<String> deleteAddressBook(Long ids){

        addressBookService.removeById(ids);

        return R.success("删除成功");
    }
}
